package collection;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class MapList extends JInternalFrame {
	private JTextField inSearch;
	private JButton btnSearch;
	private JButton btnAllKey;
	private JButton btnAllData;
	private JScrollPane scrollPane;
	private JTextArea result;

	HashMap<String, MapData> lhm;
	private JLabel lblNewLabel;
	private JButton btnNewButton;
	
	
	public MapList(HashMap<String, MapData> a) {
		super("List", true, true, true, true);
		
		this.lhm=a;
		
		getContentPane().setBackground(Color.GRAY);
		setTitle("List");
		setBounds(100, 100, 282, 442);
		getContentPane().setLayout(null);
		getContentPane().add(getInSearch());
		getContentPane().add(getBtnSearch());
		getContentPane().add(getBtnAllKey());
		getContentPane().add(getBtnAllData());
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnNewButton());

	}

	private JTextField getInSearch() {
		if (inSearch == null) {
			inSearch = new JTextField();
			inSearch.setBounds(12, 10, 116, 21);
			inSearch.setColumns(10);
		}
		return inSearch;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(inSearch.getText().equals("")){
						JOptionPane.showMessageDialog(null, "ID를 입력하세요.");
						return;
					}
					try{
						String ki = inSearch.getText();
						result.append("ID"+"     "+"NAME"+"     "+"PHONE"+"     "+"가입일\n");
						result.append(lhm.get(ki).toString());						
					}catch(Exception ex){
						
					}
					
					
				}
			});
			btnSearch.setBounds(140, 9, 116, 23);
		}
		return btnSearch;
	}
	private JButton getBtnAllKey() {
		if (btnAllKey == null) {
			btnAllKey = new JButton("All Key");
			btnAllKey.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					result.setText("");
					int cnt=0;
					Set <String> sk = lhm.keySet();
					Iterator<String> it = sk.iterator();
					
					result.append("=====All Key List=====\n");
					while(it.hasNext()){
						cnt++;
						String temp = it.next();						
						result.append("No."+cnt+"'s Key : "+temp+"\n");
					}
					
				}
			});
			btnAllKey.setBounds(12, 41, 116, 23);
		}
		return btnAllKey;
	}
	private JButton getBtnAllData() {
		if (btnAllData == null) {
			btnAllData = new JButton("All Data");
			btnAllData.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					result.setText("");
					result.append("=====All Data List=====\n");
					result.append("ID"+"     "+"NAME"+"     "+"PHONE"+"     "+"가입일\n");
					
					int cnt=0;
					Set <String> sk = lhm.keySet(); // 모든 키 값을 Set에 넘긴다. 순서는 상관 X
					Iterator<String> it = sk.iterator(); // 접근자로 iterator 사용
										
					while(it.hasNext()){
						cnt++;
						String temp = it.next();
						result.append(lhm.get(temp).toString());						
					}					
				}
			});
			btnAllData.setBounds(140, 41, 116, 23);
		}
		return btnAllData;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 74, 242, 299);
			scrollPane.setViewportView(getResult());
			scrollPane.setColumnHeaderView(getLblNewLabel());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel();
		}
		return lblNewLabel;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("Clear");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					result.setText("");
				}
			});
			btnNewButton.setBounds(12, 379, 242, 23);
		}
		return btnNewButton;
	}
}
